package com.alliance.shuwei.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@MappedSuperclass
public abstract class Auditable {

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created", nullable = false)
    protected Date createdDate;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "modified", nullable = true)
    protected Date modifiedDate;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "deleted", nullable = true)
    protected Date deletedDate;

    
    public Auditable() {
        super();
    }
   
    public Date getCreatedDate() {
        return createdDate == null ? null : new Date(createdDate.getTime());
    }

    
    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate == null ? null : new Date(createdDate.getTime());
    }

    
    public Date getModifiedDate() {
        return modifiedDate == null ? null : new Date(modifiedDate.getTime());
    }

    public void setModifiedDate(Date modifiedDate) {
        this.modifiedDate = modifiedDate == null ? null : new Date(modifiedDate.getTime());
    }

    public Date getDeletedDate() {
        return deletedDate == null ? null : new Date(deletedDate.getTime());
    }

    public void setDeletedDate(Date deletedDate) {
        this.modifiedDate = deletedDate == null ? null : new Date(deletedDate.getTime());
    }

    // -- Lifecycle Callbacks --

    @PrePersist
    public void updateCreatedAuditColumns() {
        Date now = new Date();
        setCreatedDate(now);
        setModifiedDate(now);
    }

    @PreUpdate
    @PreRemove
    public void updateModifiedAuditColumns() {
        setModifiedDate(new Date());
    }
}
